﻿<%@ Page Title="" Language="C#" MasterPageFile="~/Views/Shared/Site.Master" Inherits="System.Web.Mvc.ViewPage<dynamic>" %>

<asp:Content ID="Content1" ContentPlaceHolderID="PageTitle" runat="server">
    <title>每日考勤管理</title>
</asp:Content>
<asp:Content ID="Content2" ContentPlaceHolderID="PageStyles" runat="server">
    <link href="../../Content/table_jui.css" rel="stylesheet" type="text/css" />
    <style type="text/css">
        .applicationTarget
        {
            color: #008B00;
        }
        .txtRemark
        {
            word-break: break-all;
            word-wrap: break-word;
        }
    </style>
</asp:Content>
<asp:Content ID="Content3" ContentPlaceHolderID="PageScripts" runat="server">
    <script src="../../Scripts/jquery.dataTables.js" type="text/javascript"></script>
    <link href="../../Content/Attendance/attendance.css" rel="stylesheet" type="text/css" />
    <script type="text/javascript">
    //************* URL常量
    var URL_ = "";
    //************* 全局变量
    var g_Var = {
        "time": null,
        "isNeedToWork": true,
        "currentDate": null,
        "isDisplayEvaluationInfo": null,
        "userIdList": [],
        
    };
    //************* 常量定义
    var StaticResource = {
        "WeekendInfo": {
            "false": "非周末",
            "true": "周末"
        },
        "IsNeedToWork": {
            "false": "休息",
            "true": "正常上班"
        },
        "AttendanceType": {
            "onDuty": "上班: ",
            "offDuty": "下班: "
        },
        "AttendanceEvaluation": {
            "Late": "迟到: ",
            "LeaveEarly": "早退: ",
            "Absense": "[旷工]",
            "ExtraWork": "[加班]",
            "ManHours": "获得工时: "
        },

        "timeTarget": {
            "minute": "分钟",
            "hour": "小时"
        },


        "leaveInfo": {
            "leaveForWorkOrNot": {
                "true": "[工作假期]",
                "false": "[个人假期]"
            },

            "leaveTime": "时间: ",
            "leaveType": "类别: "
        }
    };
    
    var AttdendacneTypeResource = {
        'onDuty': StaticResource.AttendanceType.onDuty,
        'offDuty': StaticResource.AttendanceType.offDuty
    };


    var convertor = {
        "员工": function (item) {
            g_Var.userIdList.push(item.TargetUser.Id);

            var targetUserName = item.TargetUser.Name;
            var targetUserDeptName = item.TargetUser.Dept.DeptName;
            return targetUserName + "<br/>" + "(" + targetUserDeptName + ")";
        },
        "考勤记录": function (item) {
            //打卡所有记录
            var attendanceDetail = " -- ";
            var attendanceList = item.AttendanceInfoList;
            for (var key in attendanceList) {
                //上班
                if (attendanceList[key].AttendanceType == "onDuty") {
                    attendanceDetail = AttdendacneTypeResource.onDuty 
                        + Date.fromEpoch(attendanceList[key].ActionTime).toFullTimeFormat() 
                        + "<br\>";
                }
                // 下班：
                else {
                    attendanceDetail = AttdendacneTypeResource.offDuty 
                        + Date.fromEpoch(attendanceList[key].ActionTime).toFullTimeFormat()
                        + "<br\>";
                }
            }
           
            return attendanceDetail;
        },

        "假期": function (item) {
            var leaveInfo = " -- ";
            var brTarget = "<br\>"

            if (item.LeaveInfo) {
                var leaveList = item.LeaveInfo.Leaves;
                for (var key in leaveList) {
                    var isForWork = leaveList[key].IsForWork;
                    var startTime = Date.fromEpoch(leaveList[key].StartTime);
                    var endTime = Date.fromEpoch(leaveList[key].EndTime);
                    var startTimeFormat = startTime.toFullTimeFormat();
                    var endTimeFormat = endTime.toFullTimeFormat();

                    var leaveTime = startTimeFormat + " 至 " + endTimeFormat;

                    //todo:
                    //太多了
                    leaveInfo = StaticResource.leaveInfo.leaveType
                       + leaveList[key].LeaveTypeName
                       + StaticResource.leaveInfo.leaveForWorkOrNot[isForWork]
                       + brTarget
                       + StaticResource.leaveInfo.leaveTime
                       + leaveTime
                       + brTarget
                }
            }

            return leaveInfo;
        },

        "考勤评估": function (item) {
            var evaluationStr = item.EvaluationStr;
            var applicationStr = item.Application;
            var modifyStr = item.ModifyHistoryStr;
            if(applicationStr.length != 0)
                evaluationStr = evaluationStr + "</br>" + applicationStr;
            if(modifyStr.length != 0)
                evaluationStr = evaluationStr + "</br>" + modifyStr;
            return evaluationStr;
        },

        "备注": function (item) {
            var remarkStr = " -- ";
            if(item.RemarkInfo){
                remarkStr =  item.RemarkInfo.Remark; 
            }
            return remarkStr;
        },
       
        "状态": function (item) {
            return  item.EvaluationClassName;
        }
    };
    //************* Accessor访问器
    var EzAccessor = {};
    function CreateAccessor() {
        if (ezmock.IS_MOCKED) {
            EzAccessor = CreateMockEzAccessor();

        } else {
            EzAccessor = CreateRealEzAccessor();
        }
    }



    function CreateRealEzAccessor() {
        //实现服务器交互方法
        var realEzAccessor = {
            "getDayInfo": function () {
                                return <%=ViewData["DayInfo"]%>;
            },
            "getIsDisplayAttendanceInfo": function () {

                                return <%=ViewData["IsDisplayAttendanceInfo"]%>;
            },

            "getDayAttendanceDetailList": function () {
                                return <%=ViewData["DayAttendanceDetailList"]%>;
            },
            "getIsDisplayEvaluationInfo": function () {

                                return <%=ViewData["IsDisplayEvaluationInfo"]%>;
            },


        };

        return realEzAccessor;
    }

    function getAttendanceType(bool, AttendanceType) {
        var attendanceType = StaticResource.AttendanceType[AttendanceType];
        //是否正常考勤
        if (bool) {
            attendanceType = '<span class="important">*</span>' + attendanceType;
        }

        return attendanceType;

    }



    function getAttendanceEvaluationString(attendanceEvaluation, isDisplayEvaluationInfo) {
        var evaluationInfo = "";
        if(isDisplayEvaluationInfo){
            var late = StaticResource.AttendanceEvaluation.Late;
            var leaveEarly = StaticResource.AttendanceEvaluation.LeaveEarly;
            var absence = StaticResource.AttendanceEvaluation.Absense;
            var extraWork = StaticResource.AttendanceEvaluation.ExtraWork;
            var minuteTarget = StaticResource.timeTarget.minute;

            var brTarget = "<br\>";
            
            if (g_Var.isNeedToWork) {
                //迟到
                if (attendanceEvaluation.LateMiniutes > 0) {
                    evaluationInfo = evaluationInfo
                            + late
                            + attendanceEvaluation.LateMiniutes
                            + minuteTarget
                            + brTarget;
                }
                //早退
                if (attendanceEvaluation.EarlyMiniutes > 0) {
                    evaluationInfo = evaluationInfo
                            + leaveEarly
                            + attendanceEvaluation.EarlyMiniutes
                            + minuteTarget
                            + brTarget;
                }
            }
            //旷工
            if (attendanceEvaluation.IsAbsense) {
                evaluationInfo = evaluationInfo
                            + absence
                            + brTarget;

            }
            //加班
            if (attendanceEvaluation.IsExtraWork) {
                evaluationInfo = evaluationInfo
                            + extraWork
                            + brTarget;
            }    
        }
        return evaluationInfo;
    }



    function setTableColumn() {
        var userColumnHead = $('#tblAttendanceList thead tr th')[0];
        $(userColumnHead).css({
            'width': '88px'
        });

        var actionTimeColumnHead = $('#tblAttendanceList thead tr th')[1];
        $(actionTimeColumnHead).css({
            'width': '98px'
        });

        var leaveColumnHead = $('#tblAttendanceList thead tr th')[2];
        $(leaveColumnHead).css({
            'width': '86px'
        });

        var evluationColumnHead = $('#tblAttendanceList thead tr th')[3];
        $(evluationColumnHead).css({
            'width': '100px'
        });
    }


    function bindAttendanceList(originalJson) {
        var data = DataConverterForDataTable.convertData(originalJson, convertor);

        data.bPaginate = false;
        data.bRetrieve = true;
        data.bFilter = false;
        data.bSort = false;
        data.bInfo = false;
        
        data.fnRowCallback = function (nRow, aData, iDisplayIndex, iDisplayIndexFull) {
            $($('td', nRow)[5]).remove()
            var calssName = aData[5];

            $(nRow).removeClass('odd');
            $(nRow).removeClass('even');
            $(nRow).addClass(calssName);
            
            return nRow;
        };

        data.fnHeaderCallback = function (nHead, aasData, iStart, iEnd, aiDisplay) {
            // 隐藏第6列
            $($('th', nHead)[5]).remove();
        }


        $('#cmbAttendanceListPerDay')
          .empty();
        var tblAttendanceList = $('<table id="tblAttendanceList" class="tbl"/>')
          .appendTo('#cmbAttendanceListPerDay');
        $('#tblAttendanceList')
          .dataTable(data)

        //设置表格列宽
        setTableColumn();
        //条目事件
        if(originalJson){
            setColumnEvent();
        }
    }

    function setColumnEvent(){
        $('#tblAttendanceList tbody tr').each(function (index) {
                var _self = $(this);
                var _index = index;
                var backgroundColor = _self.css('background-color');
                _self.hover(function () {
                    setTRCss(_self, '#e5e5e5', 'pointer');
                }, function () {
                    setTRCss(_self, backgroundColor, 'default');
                });

                _self.click(function () {
                    var userId = g_Var.userIdList[_index];
                    window.location.href = "ModifyAttendanceInfoPerDay?UserId="
                           + userId
                           + "&time="
                           + g_Var["time"];
                });
        });
    }

     function setTRCss(tr, color , mouseStatus) {
        $(tr).css({
            "background-color": color,
            "cursor" : mouseStatus,
        });    
    };

   
    function showDayInfo(dayInfo) {
        var isWeekEnd = StaticResource.WeekendInfo[dayInfo.IsWeekEnd];
        var needToWork = StaticResource.IsNeedToWork[dayInfo.IsNeedToWork];
        var extraShiftInfo = "";
        var holidayInfo = "";

        if (dayInfo.ExtraShiftInfo && dayInfo.HolidayInfo) {
            extraShiftInfo = dayInfo.ExtraShiftInfo.Name;
            holidayInfo = dayInfo.HolidayInfo.Name;
        }

        var dayInfoDom = '<p>当日信息 : {weekendInfo}  {holidayInfo}  {extraShiftInfo} {needToWork}</p>';
        dayInfoDom = dayInfoDom.replace("{weekendInfo}", isWeekEnd)
                           .replace("{holidayInfo}", holidayInfo)
                           .replace("{extraShiftInfo}", extraShiftInfo)
                           .replace("{needToWork}", needToWork);

        $('#lblDayInfo').append(dayInfoDom);
    }



    function showAttendanceDetailTittle(dayInfo) {
        var nowDay = Date.fromEpoch(dayInfo.Date).toFullChineseDateFormat();
        var tittleStr = nowDay + " " + "全体员工考勤详细";

        $('#lblTittle').html(tittleStr);
    }



    function initPage() {
        EzTipsManager.addTipBox('#tip_Remind', 'tipRemind');
        EzTipsManager.showTipBox('tipRemind');

        CreateAccessor();
        var dayInfo = EzAccessor.getDayInfo();
       
        var isDisplayAttendanceInfo = EzAccessor.getIsDisplayAttendanceInfo();

        g_Var["time"] = dayInfo.Date;
        //当日信息
        showDayInfo(dayInfo);
        showAttendanceDetailTittle(dayInfo);

        if (isDisplayAttendanceInfo) {
            //是否显示考情信息
            g_Var.isDisplayEvaluationInfo = EzAccessor.getIsDisplayEvaluationInfo();
            var attendanceList = EzAccessor.getDayAttendanceDetailList();
            bindAttendanceList(attendanceList);
            //未来时间不可查看
            if(!g_Var.isDisplayEvaluationInfo){
                $('#nextDay').hide();    
            }
        }
       
        else {
            $('#tblNoFuture').show();
            $('#cmbAttendanceListPerDay').hide();
        }


    }
    function initPageEvent() {

        $('#preDay').click(function () {
            var currentDate = Date.fromEpoch(g_Var["time"]);

            var perDay = new Date(
                currentDate.getFullYear(),
                currentDate.getMonth(),
                currentDate.getDate() - 1,
                0,
                0,
                0
            ).toEpoch();

            window.location.href = "AttendanceInfoListPerDayManager?time="
                           + perDay;

        });


        $('#nextDay').click(function () {
            var currentDate = Date.fromEpoch(g_Var["time"]);

            var nextDay = new Date(
                currentDate.getFullYear(),
                currentDate.getMonth(),
                currentDate.getDate() + 1,
                0,
                0,
                0
            ).toEpoch();

            window.location.href = "AttendanceInfoListPerDayManager?time="
                           + nextDay;
        });


    }
    $(function () {
        initPage();
        initPageEvent();
    });
    </script>
</asp:Content>
<asp:Content ID="Content4" ContentPlaceHolderID="PageHead" runat="server">
    <%
        Html.RenderPartial("_sayHello"); %>
</asp:Content>
<asp:Content ID="Content5" ContentPlaceHolderID="PageContent" runat="server">
    <div id="page-head" class="page-head">
        <img src="../../Content/icons/attendance_manager.png" style="width: 48px; height: 48px;"
            alt="manager" />
        <span id="lblTittle" class="title"></span>
        <div style="margin-top: 5px; min-height: 20px;">
            <div style="float: left;">
                <a id="preDay">前一天</a>
            </div>
            <div style="float: right;">
                <a id="nextDay">后一天</a>
            </div>
        </div>
    </div>
    <div id="page-body">
        <div id="lblDayInfo" style="margin: 20px; color: gray;">
        </div>
        <div id="tblNoFuture" style="display: none;">
            <%Html.RenderPartial("_noFutureDisplayInfo"); %>
        </div>
        <div id="cmbAttendanceListPerDay">
        </div>
    </div>
    <div class="page-submit">
    </div>
</asp:Content>
<asp:Content ID="Content6" ContentPlaceHolderID="PageShortcut" runat="server">
</asp:Content>
<asp:Content ID="Content7" ContentPlaceHolderID="PageFoot" runat="server">
    <div id="tip_Remind" style="display: none;">
        <h1>
            小贴士:</h1>
        <p class="indent">
            这里显示的是某天全体员工的考勤相关信息，包括上下班考勤记录，假期情况以及考勤评估。
        </p>
        <p class="indent">
            点击列表后面的“详细”连接，可以查看并且修改该员工详细的当日考勤相关信息。
        </p>
    </div>
</asp:Content>
